package p2pcdnsim.network;

import java.util.LinkedList;
import java.util.List;

public class DropTail implements Queue {

	//In Bytes
	public static int DEFAULT_CAPACITY = 500000;
	
	private int capacity;
	private List<Packet> queue;
	private int currBytes;
	
	/**
	 * 
	 * @param capacity - in bytes
	 */
	public DropTail(int capacity) {
		this.setCapacity(capacity);
		this.queue = new LinkedList<Packet>();
		this.currBytes = 0;
	}
	
	public DropTail() {
		this(DEFAULT_CAPACITY);
	}

	public boolean addPacket(Packet p) {
		if( capacity !=0 && currBytes+p.getSize() > capacity)
			return false;
		currBytes+=p.getSize();
		queue.add(p);
		return true;
	}

	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}

	public int getCapacity() {
		return capacity;
	}

	public Packet getNextPacket() {
		if(queue.size() == 0) return null;
		Packet p = queue.remove(0);
		currBytes -= p.getSize();
		return p;
	}

}
